var body = document.body;

exports.setTitle = function(s){
    if(!s) return ;
    document.title = s;
}

exports.toArray = function(arr){      // arr
    if(!arr) return [];
    return Array.prototype.slice.call(arr, 0);
}

exports.setBg = function(c){
    body.style.background = c;
}

exports.clearBg = function(){
    body.style.background = '';
}

/* async loading js */
exports.createJs = function(path, callback){
    if(!path) return ;
    var jsFile = document.createElement('script');
    jsFile.type = 'text/javascript';
    jsFile.src = path;
    console.log(path)
    if(callback){
        jsFile.onload = function(){
            callback();
        }
    }
    document.body.appendChild(jsFile);
}

exports.dateFormat = function(date,format) {
    if (!date) {
        return;
    }
    var o = {
        "M+": date.getMonth() + 1,
        "d+": date.getDate(),
        "h+": date.getHours(),
        "m+": date.getMinutes(),
        "s+": date.getSeconds(),
        "q+": Math.floor((date.getMonth() + 3) / 3),
        "S": date.getMilliseconds()
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;

}


function stringify(obj) {
	return obj ? Object.keys(obj).sort().map(function (key) {
		var val = obj[key];
		// 如果值是object ，转化成字符串
		if (typeof val === 'object') {
			return key + '=' + JSON.stringify(val);
		} else {
			return key + '=' + val;
		}
	}).filter(function (x) {
		return x.length > 0;
	}).join('&') : '';
}

/*
arr: [{url: '', fileType: 1, param: 'image/resize,m_fixed,h_100,w_100'}]   fileType: 0, 默认图片 1，音频 2，视频
 */

exports.getOSSFiles = function(arr,cb=function(){}){

	var hArr = arr.map((one, index)=>{
		//var type = mediaJudge(one.fileType),
		var type = one.fileType,
			i = {};

		i.url = one.url
		if(type==1 && type==2){
			i.param = '';
		}
		return i;
	});

	axios.post('/api/system/getFullUrl', stringify({
		fileObject: hArr
	})).then((res) => {
		var d = res.data;
		if(d.flag == 1){
			cb(d.data.url.split(','));
		}else{
			cb([]);
		}
	});
}




exports.getFullOssFiles = function (arr,callBack=function(){}) {
    axios.post('/api/system/getFullUrl', stringify({
        fileObject: arr
    })).then((res) => {
        var d = res.data;
        if(d.flag == 1){
            callBack(d.data.url.split(','));
        }else{
            this.$message.error(d.msg)
        }
    });
}

